//
//  MGNavigator.m
//  Express
//
//  Created by lhongl on 2025/9/28.
//  Copyright © 2025 QM. All rights reserved.
//

#import "MGNavigator.h"

@implementation MGNavigator

#pragma mark - window
+ (UIWindow *)window {
    return [[[UIApplication sharedApplication] delegate] window];
}

#pragma mark - 根ViewController
+ (UIViewController *)rootViewController {
    return [[self window] rootViewController];
}

#pragma mark - 当前viewController
+ (UIViewController *)currentViewController {
    UIViewController *rootViewController = [self rootViewController];
    return [self findCurrentViewControllerFrom:rootViewController];
}

/// - 查找最顶部界面
+ (UIViewController *)findCurrentViewControllerFrom:(UIViewController *)vc{
    UIViewController *currentViewController;
    if ([vc isKindOfClass:[UITabBarController class]] ) {
        currentViewController = [self findCurrentViewControllerFrom:[(UITabBarController *)vc selectedViewController]];
    }else if ([vc isKindOfClass:[UINavigationController class]]){
        UIViewController *nextRootVC = [(UINavigationController *)vc visibleViewController];
        currentViewController = [self findCurrentViewControllerFrom:nextRootVC];
    }else{
        currentViewController = vc;
        while (currentViewController.presentedViewController) {
            currentViewController = vc.presentedViewController;
        }
    }
    return currentViewController;;
}

#pragma mark - push
+ (void)pushViewController:(UIViewController *)viewController {
    UIViewController *currentVC = [self currentViewController];
    if (currentVC.navigationController) {
        [currentVC.navigationController pushViewController:viewController animated:YES];
    } else {
        [self presentViewController:viewController];
    }
}


#pragma mark - present
+ (void)presentViewController:(UIViewController *)viewController{
    if (!viewController) {
        return;
    }
    UIViewController *cureeVC = [self currentViewController] ;
    viewController.modalPresentationStyle = UIModalPresentationFullScreen;
    [cureeVC presentViewController:viewController animated:YES completion:NULL];
}

#pragma mark - 返回到指定界面
+ (void)goBackToViewContrller:(Class)cls{
    [self goBackToViewContrller:cls completion:NULL];
}

+ (void)goBackToViewContrller:(Class)cls completion:(void(^ __nullable)(void))completion {
    if (!cls) {
        return;
    }
    UIViewController *currentVC = [self currentViewController];
    if(currentVC.navigationController){
        UINavigationController *nav = currentVC.navigationController;
        UIViewController *targetVC = nil;
        for (UIViewController *vc in nav.viewControllers) {
            if ([vc isKindOfClass:[cls class]]) {
                targetVC = vc;
            }
        }
        if (targetVC) {
            [nav popToViewController:targetVC animated:YES];
            if (completion) {
                completion();
            }
        }
    }else {
#if DEBUG
        NSAssert(NO, @"[MG_DEBUG] currentViewController navigationController is nil");
#endif
    }
}

#pragma mark - 返回到根目录
+ (void)gobackToRoot:(BOOL)animated{
    UIViewController *viewController = [self currentViewController];
    if (viewController.navigationController) {
        if (viewController != viewController.navigationController.viewControllers.firstObject) {
            [viewController.navigationController popToRootViewControllerAnimated:animated];
        }else if (viewController.navigationController.presentingViewController) {
            [viewController.navigationController dismissViewControllerAnimated:animated completion:^{
                if ([self currentViewController].navigationController) {
                    [[self currentViewController].navigationController popToRootViewControllerAnimated:NO];
                }
            }];
        }
    }else if (viewController.presentingViewController){
        [viewController.navigationController dismissViewControllerAnimated:animated completion:^{
            if ([self currentViewController].navigationController) {
                [[self currentViewController].navigationController popToRootViewControllerAnimated:NO];
            }
        }];
    }
}

#pragma mark - 返回上一页
+ (void)goback:(BOOL)animated {
    UIViewController *viewController = [self currentViewController];
    if (viewController.navigationController) {
        if (viewController != viewController.navigationController.viewControllers.firstObject) {
            [viewController.navigationController popViewControllerAnimated:animated];
        } else if (viewController.navigationController.presentingViewController) {
            [viewController.navigationController dismissViewControllerAnimated:animated completion:NULL];
        }
    } else if (viewController.presentingViewController) {
        [viewController dismissViewControllerAnimated:animated completion:NULL];
    }
}

@end
